package com.zj.easy.ui.dialog

import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.zj.easy.ui.R

class DialogCommon() : DialogBase() {

    override fun setContentView(layoutResID: Int) {
        super.setContentView(R.layout.dialog_common)
    }

    fun contentView(view: View = View.inflate(context, R.layout.dialog_common, null)): DialogCommon {
        setContentView(view)
        return this
    }

    fun setTitle(title: String): DialogCommon {
        findViewById<TextView>(R.id.tv_title).setText(title)
        return this
    }

    fun setContent(content: String): DialogCommon {
        findViewById<TextView>(R.id.tv_content).setText(content)
        return this
    }

    fun setConfirm(confirm: String, listener: View.OnClickListener? = null): DialogCommon {
        val tv = findViewById<TextView>(R.id.tv_confirm)
        tv.setText(confirm)
        tv.setOnClickListener {
            if (listener == null) {
                dismiss()
            } else {
                listener.onClick(it)
            }
        }
        return this
    }

    fun setCancel(cancel: String = "取消", listener: View.OnClickListener? = null): DialogCommon {
        val tv = findViewById<TextView>(R.id.tv_cancel)
        tv.visibility = View.VISIBLE
        tv.setText(cancel)
        tv.setOnClickListener {
            if (listener == null) {
                dismiss()
            } else {
                listener.onClick(it)
            }
        }
        return this
    }

    fun getTitle(): TextView {
        return findViewById(R.id.tv_title)!!
    }

    fun getContent(): TextView {
        return findViewById(R.id.tv_content)!!
    }

    fun getConfirm(): TextView {
        return findViewById(R.id.tv_confirm)!!
    }

    fun getCancel(): TextView {
        return findViewById(R.id.tv_cancel)!!
    }

    fun getRootView(): ViewGroup {
        return findViewById(R.id.root_view)!!
    }

}